<?php

require_once 'Domain51/Service/Amazon/Util/AuthorizationGenerator.php';

class Domain51_Service_Amazon_Util_Header
{
    private $_headers = array();
    private $_type = '';
    private $_url = '';
    
    
    public function __construct($config = null) 
    {
        if (!empty($config['authGen'])) {
            $this->_authGen = $config['authGen'];
        }
    }
    
    public function toArray($type = '') 
    {
        if ($type == 'Amz') {
            $return = array();
            foreach ($this->_headers as $key => $value) {
                if (preg_match('/^X-Amz/', $key)) {
                    $return[$key] = $value;
                }
            }
            return $return;
        }
        
    
        if (empty($this->_authGen)) {
            $this->_authGen = new Domain51_Service_Amazon_Util_AuthorizationGenerator();
        }
        $this->_authGen->setHeader($this);
        
        $return = array();
        $return['Authorization'] = $this->_authGen->generate();
        $return = array_merge($return, $this->_headers);
        
        return $return;
    }
    
    public function setType($type) 
    { 
        $this->_type = $type;
    }
    
    public function getType() 
    { 
        return $this->_type; 
    }
    
    public function addAmz($type, $value) 
    {
        $amzType = 'X-Amz-' . $type;
        $this->add($amzType, $value);
    }
    
    public function add($type, $value) 
    { 
        $this->_headers[$type] = $value;
    }
    
    public function get($type) 
    {
        return isset($this->_headers[$type]) ? $this->_headers[$type] : null;
    }
    
    public function setUrl($url) 
    {
        $this->_url = $url;
    }
    
    public function getUrl() 
    {
        return $this->_url;
    }
    
    public function getAmz($type) 
    {
        return $this->get('X-Amz-'. $type);
    }
}

?>